package com.alinesno.cloud.base.logger.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alinesno.cloud.base.logger.entity.LogTriggerEntity;
import com.alinesno.cloud.base.logger.service.ILogTriggerService;
import com.alinesno.cloud.common.core.constants.SpringInstanceScope;
import com.alinesno.cloud.common.facade.pageable.DatatablesPageBean;
import com.alinesno.cloud.common.facade.response.ResponseBean;
import com.alinesno.cloud.common.web.basic.auth.advice.TranslateCode;
import com.alinesno.cloud.common.web.basic.auth.controller.LocalMethodBaseController;

/**
 * <p>预警触发 前端控制器 </p>
 *
 * @author WeiXiaoJin
 * @since 2020-01-26 09:30:11
 */
@Controller
@Scope(SpringInstanceScope.PROTOTYPE)
@RequestMapping("base/logger/logTrigger")
public class LogTriggerController extends LocalMethodBaseController<LogTriggerEntity, ILogTriggerService> {


	//日志记录
	private static final Logger log = LoggerFactory.getLogger(LogTriggerController.class);

	@Autowired
	private ILogTriggerService logTriggerService; 

	@TranslateCode(value="[{hasStatus:has_status}]")
	@ResponseBody
	@PostMapping("/datatables")
    public DatatablesPageBean datatables(HttpServletRequest request , Model model ,  DatatablesPageBean page){
		log.debug("page = {}" , ToStringBuilder.reflectionToString(page));
		return this.toPage(model, this.getFeign() , page) ;
    }
	
	@Override
	public void add(Model model, HttpServletRequest request) {
		// 条件规则
		Map<String , String> rules = new HashMap<String, String>() ; 
		
		rules.put("fail", "失败次数") ; 
		rules.put("max", "最大时间") ; 
		rules.put("rps", "每分钟") ; 
		
		model.addAttribute("rules", rules) ; 
		model.addAttribute("maxRule", 2) ; 
	}

	@Override
	public void modify(HttpServletRequest request, Model model, String id) {
	
		super.modify(request, model, id); 
		
		// 条件规则
		Map<String , String> rules = new HashMap<String, String>() ; 
		
		rules.put("fail", "失败次数") ; 
		rules.put("max", "最大时间") ; 
		rules.put("rps", "每分钟") ; 
		
		model.addAttribute("rules", rules) ; 
		model.addAttribute("maxRule", 2) ; 
	}

	/**
	 * 保存触发解析
	 */
	@ResponseBody
	@PostMapping("/saveTrigger")
	public ResponseBean saveTrigger(Model model, HttpServletRequest request, 
			LogTriggerEntity dto , 
			String[] conditionName , 
			String[] conditionValue , 
			String[] conditionSpel 
			) {
		logTriggerService.saveTrigger(dto , conditionName , conditionValue , conditionSpel) ; 
		return ok() ; 
	}

	@Override
	public ILogTriggerService getFeign() {
		return this.logTriggerService;
	}

}


























